﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class ConfigFunctions : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    public string roleid = "";
    protected DataTable dt = null;
    StringBuilder sb = new StringBuilder();
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Share.UserValue(0) != "admin")
            {
                Response.Redirect("Login.aspx");
            }
            CreateDataSet();
           
            InitTree(TreeView1.Nodes, 0);
        }
    }

    

    private void CreateDataSet()
    {
        DataSet ds = new DataSet();
        ds = new BLL.FUNCTIONS().GetAllFunctions();
        dt = ds.Tables[0];
    }


   

    #region 初始化树
    //从根节点开始递归调用显示子树 
    private void InitTree(TreeNodeCollection Nds, int parentId)
    {
        DataView dv = new DataView();
        TreeNode tmpNd;
        //int intId;
        dv.Table = dt;
        dv.RowFilter = "FUNC_FATHERCODE=" + parentId;
        foreach (DataRowView drv in dv)
        {
            tmpNd = new TreeNode();
            tmpNd.Value = drv["FUNC_CODE"].ToString().Trim();
            tmpNd.Text = drv["FUNC_NAME"].ToString();
            if (drv["FUNC_STOP"].ToString() == "1")
            {
                tmpNd.Checked = false;
            }
            else
            {
                tmpNd.Checked = true;
            }
            Nds.Add(tmpNd);
            //intId = Convert.ToInt32(drv["superid"].ToString());
            tmpNd.Expanded = true;
            
            InitTree(tmpNd.ChildNodes, Convert.ToInt32(tmpNd.Value));
        }
    }
    #endregion

    

    #region
    //角色加入所选的权限
    private void GetAllNodeText(TreeNodeCollection tnc)
    {
        foreach (TreeNode node in tnc)
        {
            if (node.ChildNodes.Count != 0)
                GetAllNodeText(node.ChildNodes);
            sb.Append("update FUNCTIONS set FUNC_STOP=");
            sb.Append(node.Checked ? 0 : 1);
            sb.Append(" where FUNC_CODE='");
            sb.Append(node.Value);
            sb.Append("'|");
        }
    }
    #endregion
    protected void Btn_save_Click(object sender, EventArgs e)
    {
        try
        {
            char[] charToTrim = new char[] { '|' };
            GetAllNodeText(TreeView1.Nodes);
            string stringSql = sb.ToString();
            string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);

            if (new SQLServerDAL.ORDERDOC().Deliver(sqlCommandArray))
            {
                //ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('保存成功')", true);
                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('保存成功!');location.href='ConfigFunctions.aspx';</script>");
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('保存失败')", true);
            }
        }
        catch(Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + ex.Message + "')", true);
        }

        InitTree(this.TreeView1.Nodes, 0);
    }
    
}
